/*服务器主函数——初始化数据库，解析命令*/
#include "main.h"

void gettime(char** ptime)
{
	char curtime[20];
  time_t tm;
	struct tm* p;
	
  time(&tm);
  p = localtime(&tm);
  snprintf(curtime, 20, "%d-%d-%d %2d:%2d:%2d", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
	*ptime = curtime;
}

void* subthread(void* arg)
{
	int newfd = *(int*)arg;

	/*线程分离*/
	pthread_detach(pthread_self());

	while(1)
	{
		/*接收客户端的命令*/
		read(newfd,&msg,sizeof(MSG));
		
		/*解析命令*/
		if(msg.type == 'R')
		{
			printf("client>>register\n");
			regis(newfd,&msg);
		}
		else if(msg.type == 'L')
		{
			printf("client>>login\n");
			login(newfd,&msg);
			break;
		}
		else if(msg.type == 'Q')
		{
			printf("client>>query\n");
			query(newfd,&msg);
		}
		else if(msg.type == 'H')
		{
			printf("client>>history\n");
			history(newfd,&msg);
		}
		else if(msg.type == 'E')
		{
			printf("client>>exit\n");
			printf("Client disconnected!\n");
			break;
		}
		else
			break;
	}

	/*挂断*/
	close(newfd);

	return NULL;
}

int main(void)
{
	int fd,newfd;
	int ret;
	char* logintime;
	pthread_t tid;

	/*创建数据库和表*/
	sqlite3_open("user.db",&pdb);

	//创建用户表
	strncpy(sql,"create table if not exists users (\
		username char[20] primary key,\
		password char[50],\
		lastlogin char[20]);",size);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);

	//创建历史记录表
	strncpy(sql,"create table if not exists history (\
		username char[20],\
		word char[50],\
		querytime char[20]);",size);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);	
	
	/*添加初始用户*/
	strncpy(sql,"insert into users values('root','123','2024-9-13 16:55:41');",size);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);
	strncpy(sql,"insert into users values('robin','123','2024-9-13 16:55:41');",size);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);

	/*买卡、绑卡、监听*/	
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) 
	{
		perror("socket");
		exit(1);
	}
	//重用端口号
	int on = 1;
	setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); 

	struct sockaddr_in server;
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr("192.168.104.99");
	server.sin_port = htons(12001);
	ret = bind(fd, (struct sockaddr *)&server, sizeof(server));
	if (ret == -1) 
	{
		perror("bind");
		exit(1);
	}

	listen(fd, 5); //不阻塞，最多接听5个
	printf("Server have started!\n");
	
	struct sockaddr_in client;//保存客户端的socket地址
	socklen_t len = sizeof(client);

	while(1)//并发服务器须保证while里除了accept外不阻塞
	{
		/*接听*/
		newfd = accept(fd, (struct sockaddr *)&client, &len);
		if (newfd == -1)
		{
			perror("accept");
			exit(1);
		}

		char curtime[20];
		time_t tm;
		struct tm* p;
		time(&tm);
		p = localtime(&tm);
		snprintf(curtime, 20, "%d-%d-%d %2d:%2d:%2d", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
		logintime = curtime;

		printf("Connect success with client!\n");
		printf("---------------------------------\n");
		printf("ip\t%s\n", inet_ntoa(client.sin_addr));
		printf("port\t%d\n", ntohs(client.sin_port));
		printf("time\t%s\n", logintime);//显示异常
		printf("---------------------------------\n");

		/*创建线程*/
		pthread_create(&tid,NULL, subthread, &newfd);	
	}

	/*注销卡*/
	close(fd);

	/*关闭数据库*/
	sqlite3_close(pdb);
	
	return 0;
}

